package com.cm367.teamc.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import com.cm367.teamc.bean.Holiday;
import com.cm367.teamc.bean.Rule;
import com.cm367.teamc.dao.LeaveRulesDAO;
import com.cm367.teamc.dao.PublicHolidayDAO;

@ManagedBean
@ApplicationScoped
public class LeaveRulesService {

	@ManagedProperty(value = "#{leaveRulesDAO}")
	private LeaveRulesDAO leaveRulesDAO;

	@ManagedProperty(value = "#{publicHolidayDAO}")
	private PublicHolidayDAO publicHolidayDAO;

	public LeaveRulesDAO getLeaveRulesDAO() {
		return leaveRulesDAO;
	}

	public void setLeaveRulesDAO(LeaveRulesDAO leaveRulesDAO) {
		this.leaveRulesDAO = leaveRulesDAO;
	}

	public PublicHolidayDAO getPublicHolidayDAO() {
		return publicHolidayDAO;
	}

	public void setPublicHolidayDAO(PublicHolidayDAO publicHolidayDAO) {
		this.publicHolidayDAO = publicHolidayDAO;
	}

	public boolean addTypeDays(String type, int days) {
		boolean flag = false;
		try {
			if (leaveRulesDAO.existType(type)) {
				flag = leaveRulesDAO.updateType(type, days);
			} else {
				flag = leaveRulesDAO.addTypeDays(type, days);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	public boolean deleteTypeDays(String type) {
		boolean flag = false;
		try {
			flag = leaveRulesDAO.deleteTypeDays(type);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	public boolean addPublicHoliday(String holidayName, Date date) {
		boolean flag = false;
		try {
			if (publicHolidayDAO.existHoliday(date)) {
				flag = publicHolidayDAO.updateHoliday(holidayName, date);
			} else {
				flag = publicHolidayDAO.addPubilcHoliday(holidayName, date);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	public boolean deletePublicHoliday(String date) {
		boolean flag = false;
		try {
			flag = publicHolidayDAO.deletePublicHoliday(date);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	public List<String> getTypes() {
		List<String> types = new ArrayList<String>();
		try {
			types = leaveRulesDAO.getTypes();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return types;
	}

	public int getPending(String userid, String type, int year) {
		int days = 0;
		try {
			days = leaveRulesDAO.getPending(userid, type, year);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return days;
	}

	public int getApproved(String userid, String type, int year) {
		int days = 0;
		try {
			days = leaveRulesDAO.getApproved(userid, type, year);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return days;
	}

	public int getLeft(String userid, String type, int year) {
		int days = 0;
		try {
			days = leaveRulesDAO.getLeft(userid, type, year);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return days;
	}

	public List<Rule> findAllRules() {
		List<Rule> rules = new ArrayList<Rule>();
		try {
			return leaveRulesDAO.findAllType();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rules;
	}

	public List<Holiday> findAllHolidays() {
		List<Holiday> holidays = new ArrayList<Holiday>();
		try {
			return publicHolidayDAO.findAllHoliday();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return holidays;
	}

}
